package cn.myfirefly.moming.house.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.myfirefly.moming.common.controller.BaseCommonController;
import cn.myfirefly.moming.house.model.SpfTradeContractDto;
import cn.myfirefly.moming.house.model.SpfTradeContractFilterDto;
import cn.myfirefly.moming.house.service.SpfTradeContractService;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;
import cn.myfirefly.utils.ExportUtil;
import cn.myfirefly.utils.FilterUtil;
import cn.myfirefly.utils.StrUtil;

/**
 * 回佣统计
 * @author zhouxiaoming
 */
@Controller
@RequestMapping("commissionCount")
public class CommissionController extends BaseCommonController {

	@Autowired
	private SpfTradeContractService spfTradeContractService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		return "commissioncount/index";
	}
	
	@RequestMapping("list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto<SpfTradeContractDto> list(SpfTradeContractFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		PageResultDto<SpfTradeContractDto> result = spfTradeContractService.getCommissionList(dto);
		return result;
	}
	
	@RequestMapping("export.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public ResultDto export(SpfTradeContractFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		List<SpfTradeContractDto> list = spfTradeContractService.getCommissionAllList(dto);
		String msg = ExportUtil.export(list, "CommissionCount", "回佣汇总统计", request, response);
		if (StrUtil.notBlank(msg)) {
			return error(msg);
		}
		return null;
	}
	
	@RequestMapping("sumCommission.do")
	@ResponseBody
	public ResultDto sumCommission(SpfTradeContractFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		FilterUtil.wrapFilterDto(dto, request);
		Map<String, BigDecimal> map = spfTradeContractService.getTotalCommission(dto);
		return success(map);
	}
}
